package com.love_weinuo.ui.activity;

import android.app.Activity;
import android.content.Intent;
import android.databinding.DataBindingUtil;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.View;

import com.tencent.imsdk.TIMCallBack;
import com.tencent.imsdk.TIMGroupManager;
import com.tencent.imsdk.TIMManager;
import com.tencent.qcloud.uikit.common.component.video.util.LogUtil;
import com.love_weinuo.R;
import com.love_weinuo.databinding.ActivitySettingBinding;
import com.love_weinuo.ui.BaseActivity;
import com.love_weinuo.util.DpUtil;
import com.love_weinuo.util.SpUtils;
import com.love_weinuo.util.rx.RxActivityTool;
import com.uuzuche.lib_zxing.activity.CodeUtils;

/**
 * Created by Administrator
 * on 2019/2/21.
 */

public class SettingActivity extends BaseActivity {

    ActivitySettingBinding binding;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding = DataBindingUtil.setContentView(this, R.layout.activity_setting);
        binding.setActivity(this);
        initView();
    }

    private void initView() {
        add(this);
        setBack();
        setTitleText("设置");
        binding.zxing.setOnClickListener(this);
        binding.textViewOutApp.setOnClickListener(this);
        binding.tvModuleChat.setOnClickListener(this);
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.zxing:
                int width = DpUtil.getInstance().getScreenWidth(this);
                Bitmap mBitmap = CodeUtils.createImage(new StringBuffer("通用航空").toString(),
                        width, width, BitmapFactory.decodeResource(SettingActivity.this.getResources(), R.drawable.ic_launcher_background));
                //生成二维码
                //                binding.header.setImageBitmap(mBitmap);
                break;
            case R.id.textViewOutApp:
                //单聊之退出
                TIMManager.getInstance().logout(new TIMCallBack() {
                    @Override
                    public void onError(int code, String desc) {
                        LogUtil.e("退出失败" + desc + "  失败码==>" + code);
                    }

                    @Override
                    public void onSuccess() {
                        LogUtil.e("退出成功");
                    }
                });
                SpUtils.clear(SettingActivity.this);
                RxActivityTool.finishAllActivity();
                add(new LoginActivity());
                RxActivityTool.skipActivityAndFinish(SettingActivity.this,LoginActivity.class);
                RxActivityTool.finishActivity();
                break;
            case R.id.tvModuleChat:
                LogUtil.e("当前登录用户："+ TIMManager.getInstance().getLoginUser());
                TIMGroupManager.getInstance().applyJoinGroup("1553331204411",
                        TIMManager.getInstance().getLoginUser(), new TIMCallBack() {
                            @Override
                            public void onError(final int code, final String desc) {
                                LogUtil.e("加入群组失败" + code + "  " + desc);
                                switch (code) {
                                    case 10013:
                                        LogUtil.e("已经是群员，直接聊天");
                                        ChatActivity.startGroupChat(SettingActivity.this, "1553331204411");
                                        break;
                                    default:
                                }
                            }

                            @Override
                            public void onSuccess() {
                                LogUtil.e("加入群组成功,进入聊天");
                                ChatActivity.startGroupChat(SettingActivity.this, "31045907-b971-430e-ab09-a68845ed9322");
                            }
                        });
                break;
            default:
        }
    }
}
